﻿using System;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ProjectManager.Masters.ModuleData;

namespace ProjectManager.Masters.Scrounger
{
    public partial class TabSessions : UserControl, iScroungerUserControl
    {
        public TabSessions()
        {
            InitializeComponent();
        }

        public void OnAttachData(ApplicationLibrary.DBManager.DBManager dbManager)
        {
            dgwSessions.AddReadOnlyColumn(PM_Session.Type,        true);
            dgwSessions.AddReadOnlyColumn(PM_Session.Code,        true);
            dgwSessions.AddReadOnlyColumn(PM_Session.Description, true);
            dgwSessions.AddReadOnlyColumn(PM_Session.StartDate,   true);
            dgwSessions.AddReadOnlyColumn(PM_Session.EndDate,     true);
            dgwSessions.AddReadOnlyColumn(PM_Session.TotalTime,   true);

            dgwSessions.AutoGenerateColumns = false;
            dgwSessions.DataSource = dbManager.Dataset.Tables["SESSIONSLAVE"];

            colSesType.EnumsType = typeof(Counters);
            colSesType.ResourceManager = Properties.Resources.ResourceManager;

            colSesTotalTime.Format = DateTimePickerFormat.Custom;
            colSesTotalTime.CustomFormat = "HH:mm:ss";
        }

        public void OnBindData(ApplicationLibrary.DBManager.iScrounger frm)
        {
            DocumentForm doc = (DocumentForm)frm;

            doc.BindColumn(colSesID,        PM_Session.ID);
            doc.BindColumn(colSesType,      PM_Session.Type);
            doc.BindColumn(colSesFather,    PM_Session.Code);
            doc.BindColumn(colSesDescr,     PM_Session.Description);
            doc.BindColumn(colSesStartDate, PM_Session.StartDate);
            doc.BindColumn(colSesEndDate,   PM_Session.EndDate);
            doc.BindColumn(colSesTotalTime, PM_Session.TotalTime);
            doc.BindControl(dgwSessions);
        }

        public void OnPrepareAuxData()
        {
            TimeSpan totTime = new TimeSpan();
            foreach (DataGridViewRow dr in dgwSessions.Rows)
            {
                DateTime start = DataGridViewRowValues.GetValue<DateTime>(dr, PM_Session.StartDate);
                DateTime end   = DataGridViewRowValues.GetValue<DateTime>(dr, PM_Session.EndDate);
                TimeSpan diff  = end - start;
                DataGridViewRowValues.SetValue<DateTime>(dr, PM_Session.TotalTime, Convert.ToDateTime(diff.ToString()));
                totTime += diff;
            }
            lblTotale.Text = string.Format("{0}:{1}:{2}", totTime.Days * 24 + totTime.Hours, totTime.Minutes, totTime.Seconds);
        }

        private void ChangeLabelPosition()
        {
            if (dgwSessions.Columns[PM_Session.TotalTime.Name] != null)
            {
                int idx = dgwSessions.Columns[PM_Session.TotalTime.Name].Index;
                if (dgwSessions.GetColumnDisplayRectangle(idx, false).Left != 0)
                {
                    lblTotale.Left  = dgwSessions.GetColumnDisplayRectangle(idx, false).Left;
                    lblTotale.Width = dgwSessions.GetColumnDisplayRectangle(idx, false).Width;
                }
            }
            if (lblTotale.Left == 0)
                lblTotale.Left = dgwSessions.Width - lblTotale.Width;
        }

        private void dgwSessions_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ChangeLabelPosition();
        }
    }
}